var filterCity;
var filterSex = 0; // 0 不限 1男 2女
var filterNew = 1;
var filterRecently = 0;
var category_id;

var view = new Vue({
    el: '#view',
    data: {
        myuserid: myuserid,
        imgurl: imgurl,
        ffList: [],
        ffInfo: {},
        lbList: [], // 轮播列表
        sysList: [], // 系统广播
        themeList: [], // 主题列表
        inputObj: {
            id: 0,
            uid: 0
        }, // 评论 广播id
        themeObj: {}, // 主题对象
    },
    methods: {
        // 约会报名
        joinEnroll: function (id, index) {
            var _this = this;
            var status = _this.ffList[index].self_enroll;
            var url = status == 0 ? 'home/Dymanic/enroll' : 'home/Dymanic/cancelenroll';
            _ajax(url, function (ret, err) {
                _log(JSON.stringify(ret))
                if (ret) {
                    _msg(ret.msg);
                    if (ret.code == 200) {
                        _this.ffList[index].self_enroll = status == 0 ? 1 : 0;
                        _this.ffList[index].enroll = status == 0 ? parseInt(_this.ffList[index] .enroll) + 1 : _this.ffList[index].enroll - 1;
                        pushMsg(_this.ffList[index].id)
                    } else if (ret.code == 220) {
                        _url({
                            url: 'frame4',
                            title: '开通会员'
                        })
                    }
                }
            }, {
                user_id: myuserid,
                dy_id: id,
            })
        },
        // 点赞
        zan: function (id, index, type) {
            var _this = this;
            var status = _this.ffList[index].isthumbsup;
            _ajax('home/Dymanic/thumbsup', function (ret, err) {
                _log(JSON.stringify(ret))
                _log(JSON.stringify(err))
                if (ret) {
                    _msg(ret.msg);
                    if (ret.code == 200) {
                        _this.ffList[index].isthumbsup = status == 0 ? 1 : 0;
                        _this.ffList[index].num = status == 0 ? parseInt(_this.ffList[index] .num) + 1 : _this.ffList[index].num - 1;
                        pushMsg(_this.ffList[index].id);
                    }
                }
            }, {
                type: parseInt(status) + 1,
                dy_id: id,
                userid: myuserid
            })
        },
        // 评论
        addComment: function (dy_id, uid) {
            $('.input-bg').removeClass('new-hide');
            view.inputObj = {
                id: dy_id,
                uid: uid
            };
            $('.input-bg input').focus();
        },
        // 发布评论
        send: function () {
            var _this = this;
            var val = $.trim($('.input-wrap input').val());
            if (!val) {
                _msg('评论不能为空');
                return;
            }
            _ajax('home/Dymanic/comment', function (ret, err) {
                if (ret) {
                    _msg(ret.msg);
                    if (ret.code == 200) {
                        $('.input-wrap input').val('');
                        $('.input-bg').addClass('new-hide');
                        pushMsg(_this.inputObj.uid);
                        
                    }
                }
            }, {
                dy_id: _this.inputObj.id,
                userid: myuserid,
                touserid: _this.inputObj.uid,
                type: 0, //0为评论，1为回复
                content: val,
            })
        },
        _url: function (param, url) {
            _url(param, url);
        },
        // 图片自适应
        imgCss: function (url, event) {
            _log(url)
            var e = event.currentTarget;
            imgCss(url, function (w, h) {
                if (w < h) {
                    $(e).parent().addClass('img-w').removeClass('img-h');
                }
            })
        },
        // 查看用户资料
        go_userInfo: function (id, name, sex) {
            go_userInfo(id, name, sex)
        },
        // 清除html
        clearHtml: function (s) {
            return clearHtml(s);
        }

    }
})
judgeLogin()
addCity();


_scrollToBottom(function () {
    if (getCookie('urlIndex') != 1) return;
    if (heigutgao == 1) {
        pagenum++;
        getData(pagenum)
    }
})

// 隐藏输入框
$('.input-bg input').on('blur', function () {
    $('.input-bg').addClass('new-hide');
})

// 获取数据
function getData(page) {
    var _city = filterCity == '不限地区' ? undefined : filterCity;
    var param = {
        userid: myuserid,
        count: pagesize,
        longitude: getCookie('lon'),
        latitude: getCookie('lat'),
        citys: _city,
        sex: filterSex,
        new: filterNew,
        recently: filterRecently,
        category_id: category_id,
    };
    var loading = page == 0 ? 1 : 0;
    _lists('home/Dymanic/dymaniclst', page, loading, param)
}

function selectFilter(type) {
    var buttons = type == 'time' ? ['最新发布', '最近约会'] : ['不限性别', '只看女士', '只看男士'];
    var len = parseInt(buttons.length);
    _action('', buttons, function (index) {
        if (index != (len + 1)) {
            $('#' + type + ' .' + type).text(buttons[index - 1]);

            // 时间筛选
            if (type == 'time') {
                filterNew = index == 1 ? 1 : 0;
                filterRecently = index == 1 ? 0 : 1;
            }
            // 性别筛选
            if (type == 'sex') {
                filterSex = index - 1;
            }
            getData(0);
        }
    })
}
// 增加城市
function addCity() {
    province.unshift('不限地区');
    city.unshift(['不限地区'])
    // intProvince();
}

// 切换城市
function _areaSubmit(sCity) {
    var city = sCity;
    $('.city').text(city);
    filterCity = city == '不限地区' ? undefined : city;
    getData(0);
}

var cityLen = 1; // 1 当前城市 4常驻城市
function _changeCity(city) {
    closeModalFrm();
    _areaSubmit(city);
}
// 修改当前城市
function _showCity() {
    $('#modal-wrap').removeClass('new-hide');
    _load('#modal-wrap', 'frame0/city');
}
// 隐藏模态框
function closeModalFrm() {
    $('#modal-wrap').addClass('new-hide');
}

// 获取主题列表
function getTheme() {
    _ajax('home/dymanic/category', function (ret, err) {
        if (ret && ret.code == 200 && ret.result && ret.result.length > 0) {
            view.themeList = ret.result;
            for (var i = 0, len = ret.result.length; i < len; i++) {
                view.themeObj[ret.result[i].id] = ret.result[i].name;
            }
        }
    })
}